function Player(/*tile*/ tile, /*vec2*/ position){
	this.multiItems = false; //When the player is trying to pick multiples items
	this.inventory = false;  //The inventory is open
	this.action = false;     //When the player completes his turn
	this.inactive = 0;       //When the player don't do any action
	this.run = 0;            //Waiting for run
	
	Character.call(this, tile, position);
	
	this.isNPC = false;
}

Player.prototype = Object.create(Character.prototype);
Player.prototype.constructor = Player;

Player.prototype.setMovementMessage = function(move){
	var msg = "North";
	var type = "MoveN";
	
	if (move.a == -1){
		msg = "West";
		type = "MoveW";
	}else if (move.a == 1){
		msg = "East";
		type = "MoveE";
	}else if (move.b == 1){
		msg = "South";
		type = "MoveS";
	}
	
	Console.addMessage(msg, "white", type);
};

Player.prototype.pickItem = function(item, i){
	var art = Console.getArticle(item.item.name);
	Console.addMessage("You picked " + art  + " " + item.item.name, "aqua", "picked" + item.item.name); 
	
	var ps = PlayerStats;
	item.inMap = false;
	ps.items.push(item.item);
	item.item = null;
	ps.steppedItems.splice(i,1);
	
	this.action = true;
};

Player.prototype.checkPickItems = function(){
	var ps = PlayerStats;
	if (ps.steppedItems.length == 0) return;
	
	if (ps.steppedItems.length == 1){
		if (ps.items.length == ps.pClass.itemSl){
			Console.addMessage("You can't carry any more items!", "yellow", "itemPick");
			return;			
		}
		
		var item = ps.steppedItems[0].item;
		this.pickItem(item, 0);
	}else{
		this.mapManager.cursor = 0;
		this.mapManager.page = 0;
		this.multiItems = true;
	}
};

Player.prototype.isBusy = function(){
	return this.multiItems || this.inventory;
};

Player.prototype.actions = function(){
	var game = this.mapManager.game;
	if (this.isBusy()) return;
	
	if (game.keys[67] == 1){ //C
		Console.toggleConsole();
		game.keys[67] = 2;
	}else if (game.keys[90] == 1){ //Z
		this.mapManager.game.statsX = !this.mapManager.game.statsX;
		game.keys[90] = 2;
	}else if (game.keys[13] == 1){ //Enter
		this.checkPickItems();
		game.keys[13] = 2;
	}else if (game.keys[32] == 1){ //Space
		this.act();
		game.keys[32] = 2;
	}else if (game.keys[88] == 1){ //X
		this.inventory = !this.inventory;
		this.mapManager.cursor = 0;
		this.mapManager.page = 0;
		game.keys[88] = 2;
	}
};

Player.prototype.act = function(){
	this.action = true;
	this.inactive = 0;
	Console.addMessage("PASS", "aqua", "passTurn");
};

Player.prototype.addSteppedItem = function(item, id){
	PlayerStats.steppedItems.push({
		item: item,
		id: id
	});
};

Player.prototype.removeSteppedItem = function(id){
	for (var i=0,len=PlayerStats.steppedItems.length;i<len;i++){
		if (PlayerStats.steppedItems[i].id == id){
			PlayerStats.steppedItems.splice(i, 1);
			return;
		}
	}
};

Player.prototype.equip = function(index, equip){
	var item = PlayerStats.items[index];
	for (var i=0,len=PlayerStats.items.length;i<len;i++){
		var it = PlayerStats.items[i];
		if ((it.isA && item.isA) || (it.isW && item.isW) || (it.isL && item.isL))
			it.equipped = false;
	}
	
	if (equip)
		Console.addMessage("You equip the " + item.name, "aqua", "equip_" + item.name);
	else
		Console.addMessage("You remove the " + item.name, "aqua", "unequip_" + item.name);
	item.equipped = equip;
};

Player.prototype.getEquipped = function(type){
	for (var i=0,len=PlayerStats.items.length;i<len;i++){
		var it = PlayerStats.items[i];
		if (it[type] && it.equipped)
			return it;
	}
	
	return null;
};

Player.prototype.checkMove = function(){
	var game = this.mapManager.game;
	var move = null;
	if (game.keys[37]){ move = vec2(-1,0); game.keys[37] = 2; }else
	if (game.keys[38]){ move = vec2(0,-1); game.keys[38] = 2; }else
	if (game.keys[39]){ move = vec2( 1,0); game.keys[39] = 2; }else
	if (game.keys[40]){ move = vec2(0, 1); game.keys[40] = 2; }
	
	if (move != null){
		if ((this.run == 0 || this.run >= 21) && this.moveTo(move)){
			this.setMovementMessage(move);
			this.action = true;
		}
		this.run++;
	}else if (move == null){
		this.run = 0;
	}
};

Player.prototype.step = function(){
	if (!this.isBusy()){ this.checkMove(); }
};

Player.prototype.loop = function(){
	this.action = false;
	this.inactive++;
	
	this.step();
	this.actions();
	
	this.mapManager.setViewToPlayer();
	this.draw();
	
	if (this.action){ 
		this.inactive = 0;
		FOV.getFov(this.position, this.mapManager, 6);
	}
	if (this.inactive >= 150) this.act();
	if (this.isBusy()) this.inactive = 0;
};

var PlayerStats = {
	name: "Kram",
	pClass: Classes.soldier,
	
	gold: 0,
	level: 1,
	exp: 0,
	
	hp: 15,
	mHp: 30,
	
	stam: 7,
	mStam: 15,
	
	str: 10,
	dfs: 10,
	spd: 10,
	luk: 10,
	wis: 10,
	cha: 10,
	
	items: [],
	steppedItems: [],
	
	potions: [],
	
	treasures: []
};